package com.housediyadmin.view.components
{
	
	import com.housediyadmin.events.ShowDetailEve;
	import com.housediyadmin.events.fittingEvents.AddFittingEvent;
	import com.housediyadmin.events.fittingEvents.EditFittingEvent;
	import com.housediyadmin.model.FittingModel;
	import com.housediyadmin.vo.Fitting;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;

	/**
	 * 
	 * @author wenjun cai
	 * 2010/11/24
	 */	

	public class EditFittingMediator extends Mediator
	{
		[Inject] 
		public var view:EditFittingView;
		
		[Inject]
		public var fittingModel:FittingModel;
		
		private var oldFitting:Fitting;
		private var newFitting:Fitting;
		
		override public function onRegister():void
		{
			var dateFormat:DateFormatter=new DateFormatter();
			dateFormat.formatString="YYYY年MM月DD日";
			newFitting=new Fitting();
			
			if(view.type=="ADD")
			{
				view.createDateLbl.text=dateFormat.format(new Date());
				view.editDateLbl.text=dateFormat.format(new Date());
				
				view.nameTxt.text="";
				view.descTxt.text="";
				view.creatorTxt.text="";
				view.editorTxt.text="";
			}
			else if(view.type=="EDIT")
			{
				//绑定
				oldFitting=fittingModel.selectedFitting;
				view.createDateLbl.text=dateFormat.format(oldFitting.fittingCreateDate);
				view.editDateLbl.text=dateFormat.format(new Date());
				view.nameTxt.text=oldFitting.fittingName;
				view.descTxt.text=oldFitting.fittingDesc;
				view.creatorTxt.text=oldFitting.fittingCreator;
				view.editorTxt.text=oldFitting.fittingEditor;
			}
			//确定			
			view.addEventListener(CloseEvent.CLOSE,close);
			view.cancelBtn.addEventListener(MouseEvent.CLICK,close);
			view.okBtn.addEventListener(MouseEvent.CLICK,okClk);
			
			
		}
		
		private function close(e:Event):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
		}
		
		private function okClk(e:MouseEvent):void
		{
			if(view.type=="ADD")
			{
				newFitting.fittingCreateDate=new Date();
				newFitting.fittingCreator=view.creatorTxt.text;
				newFitting.fittingEditDate=new Date();
				newFitting.fittingEditor=view.editorTxt.text;
				newFitting.fittingDesc=view.descTxt.text;
				newFitting.fittingName=view.nameTxt.text;
				
				dispatch(new AddFittingEvent(AddFittingEvent.ADD_FITTING,newFitting));
				
				Alert.yesLabel="确定";
				Alert.show("添加成功！","",1,view,okAlert);
			}
			else if(view.type=="EDIT")
			{
				oldFitting=fittingModel.selectedFitting;
				newFitting.fittingID=oldFitting.fittingID;
				newFitting.fittingCreateDate=oldFitting.fittingCreateDate;
				newFitting.fittingCreator=view.creatorTxt.text;
				newFitting.fittingEditDate=new Date();
				newFitting.fittingEditor=view.editorTxt.text;
				newFitting.fittingDesc=view.descTxt.text;
				newFitting.fittingName=view.nameTxt.text;
				
				dispatch(new EditFittingEvent(EditFittingEvent.EDIT_FITTING,newFitting));
				
				Alert.yesLabel="确定";
				Alert.show("修改成功！","",1,view,okAlert);
				
			}
//			dispatch(new ShowDetailEve(ShowDetailEve.FittingDetail,newFitting));
//			var fittingAC:ArrayCollection=view.fittingCB.dataProvider as ArrayCollection;
//			if(popup.type=="ADD")
//			{
//				for each(var fitting:Fitting in fittingAC)
//				{
//					if(fitting.fittingsName==Fitting(e.obj).fittingsName)
//					{
//						view.fittingCB.selectedItem=fitting;
//						break;
//					}
//				}
//
//			}
//			else if(popup.type=="EDIT")
//			{
//				for each(var fitting:Fitting in fittingAC)
//				{
//					if(fitting.fittingsID==Fitting(e.obj).fittingsID)
//					{
//						view.fittingCB.selectedItem=fitting;
//						break;
//					}
//				}
//			}
			
		}
		
		private function okAlert(e:CloseEvent):void
		{
			dispatch(new ShowDetailEve(ShowDetailEve.FittingDetail,newFitting));
			close(e);
		}
	}
}



		
	